package com.selenium.dinner;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

import com.thoughtworks.selenium.DefaultSelenium;

public class PreviousMenu extends Abstract{
	public void testPreviousMenu(DefaultSelenium selenium, WebDriver driver) throws Exception{
		waitForElement(selenium, BUTTON_LABEL_PREVIOUSMENU);
		driver.findElement(By.xpath(BUTTON_LABEL_PREVIOUSMENU)).click();
		
		/* To scroll page with Javascripts Function */
//		JavascriptExecutor jse = (JavascriptExecutor)driver;
//		jse.executeScript("scroll(250, 0)");
		
		waitForElement(selenium, DATEPICKER_TRIGGER);
		waitForText(5);
		driver.findElement(By.xpath(DATEPICKER_TRIGGER)).click();
		
		/*To tap previous month repeatedly */
		for(int j=0;;j++){
			driver.findElement(By.xpath(Left_Arrows)).click();
			waitForText(2);
			if (j>=3) break;
			try {if (selenium.isVisible(January)) break;} catch (Exception e){}
		}
		
		waitForElement(selenium, TEXT_25);
		driver.findElement(By.xpath(TEXT_25)).click();
		waitForText(3);
		driver.findElement(By.xpath(DATEPICKER_TRIGGER)).click();
		
		/*To tap next month repeatedly */
		for(int j=0;;j++){
			driver.findElement(By.xpath(Right_Arrows)).click();
			waitForText(3);
			if (j>=3) break;
			try {if (selenium.isVisible(April)) break;} catch (Exception e){}
		}
		
		waitForElement(selenium, TEXT_25);
		driver.findElement(By.xpath(TEXT_25)).click();
		waitForElement(selenium, Details);
		WebElement showDetails = driver.findElement(By.xpath(Details));
		/* Click Show Details and Hide Details */
		showDetails.click();
		waitForText(5);
		showDetails.click();
		
		/*TO Scroll Complain LIst*/
		waitForElement(selenium, ARROW_DOWN);
		for (int loop=0;;loop++){
			driver.findElement(By.xpath(ARROW_DOWN)).click();
			waitForText(1);
			if (loop>=6) break;
			try{if (selenium.isVisible(LAST_ARROW_DOWN)) break;} catch (Exception e){}
		}
		
		/* Click Show Details and Hide Details */
		WebElement showDetailsBottom = driver.findElement(By.xpath(Details_last));
		showDetailsBottom.click();
		waitForText(5);
		showDetailsBottom.click();

		waitForElement(selenium, VIEW_COMPLAIN);
		driver.findElement(By.xpath(VIEW_COMPLAIN)).click();
		waitForElement(selenium, ARROW_DOWN);
		for (int loop=0;;loop++){
			driver.findElement(By.xpath(ARROW_DOWN)).click();
			waitForText(1);
			if (loop>=6) break;
			try{if (selenium.isVisible(BACK)) break;} catch (Exception e){}
		}
		
		WebElement viewComplain = driver.findElement(By.xpath(VIEW_COMPLAIN));
		WebElement backButton = driver.findElement(By.xpath(BACK));
		Actions builder = new Actions(driver);
		Action dragAndDrop = builder.clickAndHold(viewComplain)
				.moveToElement(backButton)
				.release(backButton)
				.build();
		dragAndDrop.perform();
		
		waitForElement(selenium, BACK);
		driver.findElement(By.xpath(BACK)).click();
	}

}
